import 'dart:io';

import 'package:animation_wrappers/animations/faded_slide_animation.dart';
import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:lifecycle_lite/lifecycle_mixin.dart';
import 'package:xiaofanshu_flutter/config/custom_icon.dart';
import 'package:xiaofanshu_flutter/controller/mine_controller.dart';
import 'package:xiaofanshu_flutter/static/custom_color.dart';
import 'package:xiaofanshu_flutter/static/custom_string.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:xiaofanshu_flutter/utils/date_show_util.dart';
import 'package:xiaofanshu_flutter/utils/store_util.dart';
import 'package:xiaofanshu_flutter/apis/app.dart';
import 'package:xiaofanshu_flutter/model/response.dart';
import 'package:xiaofanshu_flutter/static/custom_code.dart';
import '../../../components/item.dart';
import 'package:xiaofanshu_flutter/utils/guide_service.dart';
import 'package:xiaofanshu_flutter/config/guide_config.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> with TickerProviderStateMixin {
  MineController mineController = Get.find();
  late TabController _tabController;
  
  // 引导相关
  final GlobalKey _editButtonKey = GlobalKey();
  final GlobalKey _settingsKey = GlobalKey();
  final GlobalKey _levelKey = GlobalKey();
  final GlobalKey _achievementKey = GlobalKey();
  final GlobalKey _storyTabKey = GlobalKey();
  GuideController? _guideController;
  bool _hasShownGuide = false;
  
  // 等级和成就数据
  int currentLevel = 1;
  String levelTitle = '新手';
  int achievementCount = 0;
  int completedAchievementCount = 0;

  @override
  void initState() {
    super.initState();
    mineController.appBarOpacity.value = 0.0;
    _tabController = TabController(length: 4, vsync: this, initialIndex: 0);
    _tabController.addListener(() {
      if (_tabController.indexIsChanging) {
        return;
      }
      Get.log('index: ${_tabController.index}');
      switch (_tabController.index) {
        case 0:
          mineController.notesTabType.value = 0;
          mineController.onTap(TabsType.notes);
          break;
        case 1:
          mineController.notesTabType.value = 1;
          mineController.onTap(TabsType.collects);
          break;
        case 2:
          mineController.notesTabType.value = 2;
          mineController.onTap(TabsType.likes);
          break;
        case 3:
          mineController.notesTabType.value = 3;
          mineController.onTap(TabsType.routes);
          break;
      }
    });
    
    // 初始化引导
    try {
      _guideController = Get.find<GuideController>();
      
      // ========== 监听引导触发器 ==========
      ever(_guideController!.shouldShowMineGuide, (shouldShow) {
        if (shouldShow && mounted) {
          Get.log('📢 收到 MinePage 引导触发信号');
          _hasShownGuide = false; // 重置标志
          
          // 延迟一下，确保UI完全渲染
          Future.delayed(const Duration(milliseconds: 300), () {
            if (mounted) {
              _showGuideIfNeeded();
            }
          });
          
          _guideController?.resetMineGuideTrigger();
        }
      });
      
      Get.log('✅ MinePage 引导监听器已设置');
    } catch (e) {
      _guideController = null;
      Get.log('❌ 找不到 GuideController: $e');
    }
    // =====================================
    
    // 加载等级和成就数据
    _loadAchievementData();
  }

  Future<void> _showGuideIfNeeded() async {
    if (_guideController == null || _hasShownGuide) return;
    
    // 检查全局引导状态
    bool isGuideCompleted = await GuideService.isGuideCompleted();
    if (isGuideCompleted) {
      Get.log('⏭️ 全局引导已完成，跳过 MinePage 引导');
      _hasShownGuide = true;
      return;
    }
    
    try {
      if (mounted) {
        _hasShownGuide = true;
        await Future.delayed(const Duration(milliseconds: 500));
        
        if (!mounted) return;
        
        final steps = GuideConfig.createMineGuideTargets(
          editButtonKey: _editButtonKey,
          settingsKey: _settingsKey,
          levelKey: _levelKey,
          achievementKey: _achievementKey,
          storyTabKey: _storyTabKey,
        );
        
        if (steps.isNotEmpty) {
          GuideService.showGuide(
            context: context,
            steps: steps,
            onFinish: () async {
              Get.log('✅ MinePage 引导完成');
              await GuideService.setMineGuideCompleted();
              await GuideService.setGuideCompleted();
              _guideController?.completeAllGuides();
              
              Get.snackbar(
                '恭喜',
                '您已完成所有新手引导！',
                snackPosition: SnackPosition.BOTTOM,
                duration: const Duration(seconds: 2),
              );
            },
            onSkip: () async {
              Get.log('⏭️ 用户跳过 MinePage 引导');
              await GuideService.setGuideCompleted();
              _guideController?.completeAllGuides();
            },
          );
        }
      }
    } catch (e) {
      Get.log('❌ MinePage 引导显示失败: $e');
    }
  }
  
  // 加载等级和成就数据
  Future<void> _loadAchievementData() async {
    String? userIdStr = await readData('userId');
    int userId = int.tryParse(userIdStr ?? '0') ?? 0;
    
    if (userId == 0) return;
    
    try {
      // 获取等级信息
      final levelResponse = await AchievementApi.getUserLevel(userId);
      if (levelResponse.code == StatusCode.getSuccess && levelResponse.data != null) {
        setState(() {
          currentLevel = levelResponse.data['currentLevel'] ?? 1;
          levelTitle = levelResponse.data['levelTitle'] ?? '新手';
        });
      }
      
      // 获取成就列表
      final achievementResponse = await AchievementApi.getUserAchievements(userId);
      if (achievementResponse.code == StatusCode.getSuccess && achievementResponse.data != null) {
        List<dynamic> achievements = achievementResponse.data;
        setState(() {
          achievementCount = achievements.length;
          completedAchievementCount = achievements.where((a) => a['isCompleted'] == true).length;
        });
      }
    } catch (e) {
      Get.log('加载等级和成就数据失败: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.light,
    ));
    return Obx(
      () => Scaffold(
        backgroundColor: Colors.transparent,
        extendBodyBehindAppBar: true,
        appBar: AppBar(
          backgroundColor: Colors.transparent
              .withOpacity(mineController.appBarOpacity.value),
          title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // 左侧占位，保持布局平衡
              const SizedBox(width: 48),
              mineController.isShowTopAvatar.value
                  ? FadedSlideAnimation(
                      beginOffset: const Offset(0, 1),
                      endOffset: Offset.zero,
                      fadeDuration: const Duration(milliseconds: 300),
                      slideDuration: const Duration(milliseconds: 300),
                      child: Container(
                        width: 35,
                        height: 35,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(40),
                          image: DecorationImage(
                            image: NetworkImage(
                                mineController.userInfo.value.avatarUrl),
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                    )
                  : Container(),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  IconButton(
                    icon: const Icon(
                      CustomIcon.share,
                      color: Colors.white,
                      size: 22,
                    ),
                    onPressed: () {
                      Get.snackbar(
                        '提示',
                        '分享功能还在开发中，敬请期待',
                        snackPosition: SnackPosition.TOP,
                        backgroundColor: Colors.black87,
                        colorText: Colors.white,
                        duration: const Duration(seconds: 2),
                        margin: const EdgeInsets.all(16),
                        borderRadius: 8,
                      );
                    },
                  ),
                  // 扫码按钮 - 已隐藏
                  // IconButton(
                  //   icon: const Icon(
                  //     CustomIcon.scanQR,
                  //     color: Colors.white,
                  //     size: 22,
                  //   ),
                  //   onPressed: () {
                  //     Get.snackbar('扫一扫', '扫描二维码');
                  //   },
                  // ),
                ],
              ),
            ],
          ),
          leadingWidth: 0,
          leading: Container(),
        ),
        body: RefreshIndicator(
          onRefresh: () async {
            await mineController.onRefresh();
          },
          color: CustomColor.primaryColor,
          notificationPredicate: (ScrollNotification notification) {
            return true;
          },
          child: ExtendedNestedScrollView(
            physics: const AlwaysScrollableScrollPhysics(),
            controller: mineController.scrollController,
            scrollDirection: Axis.vertical,
            headerSliverBuilder: (context, innerBoxIsScrolled) {
              return <Widget>[
                SliverToBoxAdapter(
                  child: Obx(
                    () => Container(
                      height: MediaQuery.of(context).size.height * 0.5,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            Colors.black.withOpacity(0.9), // 从上到下渐变变暗
                            Colors.transparent,
                          ],
                        ),
                        image: DecorationImage(
                          image: NetworkImage(
                              mineController.userInfo.value.homePageBackground),
                          fit: BoxFit.cover,
                        ),
                      ),
                      child: Container(
                        decoration: const BoxDecoration(
                          gradient: LinearGradient(
                            begin: Alignment.topCenter,
                            end: Alignment.bottomCenter,
                            colors: [
                              Colors.black38, // 从上到下渐变变暗
                              Colors.black87,
                            ],
                          ),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            // 顶部位置
                            SizedBox(
                              height: MediaQuery.of(context).padding.top,
                            ),
                            // 用户信息
                            GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              onTap: () {
                                Get.toNamed(
                                  '/image/backgroundPreview',
                                  arguments: mineController
                                      .userInfo.value.homePageBackground,
                                );
                              },
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Stack(
                                    children: [
                                      GestureDetector(
                                        behavior: HitTestBehavior.opaque,
                                        onTap: () {
                                          Get.toNamed(
                                            '/image/preview',
                                            arguments: mineController
                                                .userInfo.value.avatarUrl,
                                          );
                                        },
                                        child: Container(
                                          width: 80,
                                          height: 80,
                                          decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.circular(40),
                                            image: DecorationImage(
                                              image: NetworkImage(mineController
                                                  .userInfo.value.avatarUrl),
                                              fit: BoxFit.cover,
                                            ),
                                          ),
                                        ),
                                      ),
                                      Positioned(
                                        right: 0,
                                        bottom: 0,
                                        child: Container(
                                          width: 24,
                                          height: 24,
                                          decoration: BoxDecoration(
                                            color: Colors.amberAccent,
                                            borderRadius:
                                                BorderRadius.circular(15),
                                          ),
                                          child: const Icon(
                                            Icons.add,
                                            color: Colors.black,
                                            size: 16,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                  SizedBox(
                                    height: 80,
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceEvenly,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          mineController
                                              .userInfo.value.nickname,
                                          style: const TextStyle(
                                            color: Colors.white,
                                            fontSize: 18,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                        Text(
                                          '故事号：${mineController.userInfo.value.uid}',
                                          style: const TextStyle(
                                            color: Color(0xffa3a3a2),
                                            fontSize: 10,
                                          ),
                                        ),
                                      ],
                                    ).marginOnly(left: 10),
                                  ),
                                ],
                              ).paddingOnly(left: 15, right: 15),
                            ),
                            // 个人简介
                            Text(
                              mineController.userInfo.value.selfIntroduction,
                              maxLines: 3,
                              style: const TextStyle(
                                fontSize: 14,
                                color: Colors.white,
                                letterSpacing: 0.5,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ).marginOnly(top: 10, left: 15, right: 15),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Container(
                                  height: 20,
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                    color: Colors.white.withOpacity(0.2),
                                    borderRadius: BorderRadius.circular(15),
                                  ),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      mineController.userInfo.value.sex == 1
                                          ? const Icon(Icons.male,
                                              color: Colors.lightBlue, size: 12)
                                          : const Icon(Icons.female,
                                              color: Colors.pinkAccent,
                                              size: 12),
                                      Text(
                                        ' ${mineController.userInfo.value.age}岁',
                                        style: const TextStyle(
                                          color: Colors.white,
                                          fontSize: 10,
                                        ),
                                      )
                                    ],
                                  ).paddingOnly(left: 8, right: 8),
                                ).marginOnly(left: 15),
                                mineController.userInfo.value.ipAddr.isEmpty
                                    ? Container()
                                    : Container(
                                        height: 20,
                                        alignment: Alignment.center,
                                        decoration: BoxDecoration(
                                          color: Colors.white.withOpacity(0.2),
                                          borderRadius:
                                              BorderRadius.circular(15),
                                        ),
                                        child: Text(
                                          ' ${mineController.userInfo.value.ipAddr}',
                                          style: const TextStyle(
                                            color: Colors.white,
                                            fontSize: 10,
                                          ),
                                        ).paddingOnly(left: 8, right: 8),
                                      ).marginOnly(left: 10),
                              ],
                            ).marginOnly(top: 10),
                            // 等级和成就显示（放在年龄和IP后面）
                            Row(
                              children: [
                                // 等级显示
                                GestureDetector(
                                  key: _levelKey,
                                  onTap: () {
                                    Get.toNamed('/achievement/detail');
                                  },
                                  child: Container(
                                    height: 24,
                                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                                    decoration: BoxDecoration(
                                      color: Colors.white.withOpacity(0.15),
                                      borderRadius: BorderRadius.circular(12),
                                    ),
                                    child: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        const Icon(
                                          Icons.military_tech,
                                          color: Color(0xffffd700),
                                          size: 14,
                                        ),
                                        const SizedBox(width: 4),
                                        Text(
                                          'Lv.$currentLevel',
                                          style: const TextStyle(
                                            color: Colors.white,
                                            fontSize: 11,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                        const SizedBox(width: 2),
                                        Text(
                                          levelTitle,
                                          style: const TextStyle(
                                            color: Colors.white70,
                                            fontSize: 10,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ).marginOnly(left: 15),
                                const SizedBox(width: 10),
                                // 成就显示
                                GestureDetector(
                                  key: _achievementKey,
                                  onTap: () {
                                    Get.toNamed('/achievement/detail');
                                  },
                                  child: Container(
                                    height: 24,
                                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                                    decoration: BoxDecoration(
                                      color: Colors.white.withOpacity(0.15),
                                      borderRadius: BorderRadius.circular(12),
                                    ),
                                    child: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        const Icon(
                                          Icons.emoji_events,
                                          color: Color(0xffff9800),
                                          size: 14,
                                        ),
                                        const SizedBox(width: 4),
                                        Text(
                                          '$completedAchievementCount/$achievementCount',
                                          style: const TextStyle(
                                            color: Colors.white,
                                            fontSize: 11,
                                          ),
                                        ),
                                        const SizedBox(width: 2),
                                        const Text(
                                          '成就',
                                          style: TextStyle(
                                            color: Colors.white70,
                                            fontSize: 10,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ).marginOnly(top: 10),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                SizedBox(
                                  width:
                                      MediaQuery.of(context).size.width * 0.5,
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      GestureDetector(
                                          onTap: () {
                                            Get.toNamed(
                                              '/relation',
                                              arguments: 0,
                                            );
                                          },
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Text(
                                                '${mineController.userInfo.value.attentionNum}',
                                                style: const TextStyle(
                                                    color: Colors.white,
                                                    fontSize: 12),
                                              ),
                                              const Text(
                                                MineString.attention,
                                                style: TextStyle(
                                                    color: Colors.white70,
                                                    fontSize: 10),
                                              ),
                                            ],
                                          ).paddingOnly(left: 10, right: 10)),
                                      GestureDetector(
                                          onTap: () {
                                            Get.toNamed(
                                              '/relation',
                                              arguments: 1,
                                            );
                                          },
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Text(
                                                '${mineController.userInfo.value.fansNum}',
                                                style: const TextStyle(
                                                    color: Colors.white,
                                                    fontSize: 12),
                                              ),
                                              const Text(
                                                MineString.fans,
                                                style: TextStyle(
                                                    color: Colors.white70,
                                                    fontSize: 10),
                                              ),
                                            ],
                                          ).paddingOnly(left: 10, right: 10)),
                                      GestureDetector(
                                          onTap: () {
                                            Get.dialog(
                                              SimpleDialog(
                                                backgroundColor:
                                                    const Color(0xfffffffc),
                                                title: const Text('获赞和收藏',
                                                    textAlign: TextAlign.center,
                                                    style: TextStyle(
                                                      color: Colors.black,
                                                      fontSize: 18,
                                                    )),
                                                children: [
                                                  Column(
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .center,
                                                    children: [
                                                      Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .center,
                                                        children: [
                                                          const Text(
                                                            '当前发布故事数',
                                                            style: TextStyle(
                                                              color: CustomColor
                                                                  .unselectedColor,
                                                              fontSize: 12,
                                                            ),
                                                          ).marginOnly(
                                                              right: 10),
                                                          Text(
                                                            '${mineController.notesCount.value}',
                                                            style:
                                                                const TextStyle(
                                                              color:
                                                                  Colors.black,
                                                              fontSize: 12,
                                                            ),
                                                          ),
                                                        ],
                                                      ).marginOnly(
                                                          top: 10, bottom: 10),
                                                      Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .center,
                                                        children: [
                                                          const Text(
                                                            '当前获得点赞数',
                                                            style: TextStyle(
                                                              color: CustomColor
                                                                  .unselectedColor,
                                                              fontSize: 12,
                                                            ),
                                                          ).marginOnly(
                                                              right: 10),
                                                          Text(
                                                            '${mineController.praiseCount.value}',
                                                            style:
                                                                const TextStyle(
                                                              color:
                                                                  Colors.black,
                                                              fontSize: 12,
                                                            ),
                                                          ),
                                                        ],
                                                      ).marginOnly(
                                                          top: 10, bottom: 10),
                                                      Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .center,
                                                        children: [
                                                          const Text(
                                                            '当前获得收藏数',
                                                            style: TextStyle(
                                                              color: CustomColor
                                                                  .unselectedColor,
                                                              fontSize: 12,
                                                            ),
                                                          ).marginOnly(
                                                              right: 10),
                                                          Text(
                                                            '${mineController.collectCount.value}',
                                                            style:
                                                                const TextStyle(
                                                              color:
                                                                  Colors.black,
                                                              fontSize: 12,
                                                            ),
                                                          ),
                                                        ],
                                                      ).marginOnly(
                                                          top: 10, bottom: 10),
                                                      TextButton(
                                                        onPressed: () {
                                                          Get.back();
                                                        },
                                                        style: ButtonStyle(
                                                          overlayColor:
                                                              WidgetStateProperty
                                                                  .all(
                                                            Colors.transparent,
                                                          ),
                                                        ),
                                                        child: Container(
                                                          width:
                                                              double.infinity,
                                                          height: 30,
                                                          alignment:
                                                              Alignment.center,
                                                          decoration:
                                                              BoxDecoration(
                                                            color: CustomColor
                                                                .primaryColor,
                                                            borderRadius:
                                                                BorderRadius
                                                                    .circular(
                                                                        15),
                                                          ),
                                                          child: const Text(
                                                            '我知道了',
                                                            style: TextStyle(
                                                              color:
                                                                  Colors.white,
                                                              fontSize: 12,
                                                            ),
                                                          ),
                                                        ).marginOnly(top: 10),
                                                      ),
                                                    ],
                                                  ).marginOnly(
                                                      left: 25, right: 25),
                                                ],
                                              ),
                                            );
                                          },
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Text(
                                                '${mineController.praiseCount.value + mineController.collectCount.value}',
                                                style: const TextStyle(
                                                    color: Colors.white,
                                                    fontSize: 12),
                                              ),
                                              const Text(
                                                MineString.getPraiseAndCollect,
                                                style: TextStyle(
                                                    color: Colors.white70,
                                                    fontSize: 10),
                                              ),
                                            ],
                                          ).paddingOnly(left: 10, right: 10)),
                                    ],
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      GestureDetector(
                                        key: _editButtonKey,
                                        onTap: () {
                                          Get.toNamed('/editInfo',
                                              arguments: mineController
                                                  .userInfo.value.id);
                                        },
                                        child: Container(
                                          height: 30,
                                          alignment: Alignment.center,
                                          decoration: BoxDecoration(
                                            color:
                                                Colors.white.withOpacity(0.2),
                                            borderRadius:
                                                BorderRadius.circular(15),
                                          ),
                                          child: const Text(
                                            '编辑资料',
                                            style: TextStyle(
                                              color: Colors.white,
                                              fontSize: 13,
                                            ),
                                          ).paddingOnly(left: 15, right: 15),
                                        ),
                                      ),
                                      GestureDetector(
                                        key: _settingsKey,
                                        onTap: () {
                                          Get.toNamed('/settings',
                                              arguments: mineController
                                                  .userInfo.value.id);
                                        },
                                        child: Container(
                                          height: 30,
                                          alignment: Alignment.center,
                                          decoration: BoxDecoration(
                                            color:
                                                Colors.white.withOpacity(0.2),
                                            borderRadius:
                                                BorderRadius.circular(15),
                                          ),
                                          child: const Icon(
                                            Icons.settings,
                                            color: Colors.white,
                                            size: 16,
                                          ).paddingOnly(left: 15, right: 15),
                                        ).marginOnly(left: 15),
                                      ),
                                    ],
                                  ).paddingOnly(left: 10, right: 15),
                                ),
                              ],
                            ).marginOnly(top: 15),
                            Expanded(
                              flex: 1,
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  // 购物车按钮 - 已隐藏
                                  // Column(
                                  //   mainAxisSize: MainAxisSize.min,
                                  //   children: [
                                  //     Container(
                                  //       width:
                                  //           (MediaQuery.of(context).size.width -
                                  //                   50) /
                                  //               3,
                                  //       alignment: Alignment.center,
                                  //       decoration: BoxDecoration(
                                  //         color: Colors.grey.withOpacity(0.4),
                                  //         borderRadius:
                                  //             BorderRadius.circular(10),
                                  //       ),
                                  //       child: const Column(
                                  //         mainAxisAlignment:
                                  //             MainAxisAlignment.spaceBetween,
                                  //         crossAxisAlignment:
                                  //             CrossAxisAlignment.start,
                                  //         children: [
                                  //           Row(
                                  //             children: [
                                  //               Icon(
                                  //                 Icons.shopping_cart_outlined,
                                  //                 color: Colors.white,
                                  //                 size: 14,
                                  //               ),
                                  //               SizedBox(width: 5),
                                  //               Text(
                                  //                 '购物车',
                                  //                 style: TextStyle(
                                  //                   color: Colors.white,
                                  //                   fontSize: 12,
                                  //                 ),
                                  //               ),
                                  //             ],
                                  //           ),
                                  //           SizedBox(height: 5),
                                  //           Text(
                                  //             '查看推荐好物',
                                  //             style: TextStyle(
                                  //               color: Colors.white70,
                                  //               fontSize: 10,
                                  //             ),
                                  //           ),
                                  //         ],
                                  //       ).paddingOnly(
                                  //           top: 8,
                                  //           bottom: 8,
                                  //           left: 15,
                                  //           right: 15),
                                  //     ),
                                  //   ],
                                  // ),
                                  // const SizedBox(width: 10),
                                  // 创作灵感按钮 - 已隐藏
                                  // Column(
                                  //   mainAxisSize: MainAxisSize.min,
                                  //   children: [
                                  //     Container(
                                  //       width:
                                  //           (MediaQuery.of(context).size.width -
                                  //                   50) /
                                  //               3,
                                  //       alignment: Alignment.center,
                                  //       decoration: BoxDecoration(
                                  //         color: Colors.grey.withOpacity(0.4),
                                  //         borderRadius:
                                  //             BorderRadius.circular(10),
                                  //       ),
                                  //       child: const Column(
                                  //         mainAxisAlignment:
                                  //             MainAxisAlignment.spaceBetween,
                                  //         crossAxisAlignment:
                                  //             CrossAxisAlignment.start,
                                  //         children: [
                                  //           Row(
                                  //             children: [
                                  //               Icon(
                                  //                 Icons.lightbulb_outlined,
                                  //                 color: Colors.white,
                                  //                 size: 14,
                                  //               ),
                                  //               SizedBox(width: 5),
                                  //               Text(
                                  //                 '创作灵感',
                                  //                 style: TextStyle(
                                  //                   color: Colors.white,
                                  //                   fontSize: 12,
                                  //                 ),
                                  //               ),
                                  //             ],
                                  //           ),
                                  //           SizedBox(height: 5),
                                  //           Text(
                                  //             '学创作找灵感',
                                  //             style: TextStyle(
                                  //               color: Colors.white70,
                                  //               fontSize: 10,
                                  //             ),
                                  //           ),
                                  //         ],
                                  //       ).paddingOnly(
                                  //           top: 8,
                                  //           bottom: 8,
                                  //           left: 15,
                                  //           right: 15),
                                  //     ),
                                  //   ],
                                  // ),
                                  // const SizedBox(width: 10),
                                  // 浏览记录按钮 - 保留
                                  Column(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      InkWell(
                                        onTap: () {
                                          Get.toNamed('/browse/history');
                                        },
                                        borderRadius: BorderRadius.circular(10),
                                        child: Container(
                                          width:
                                              (MediaQuery.of(context).size.width -
                                                      50) /
                                                  3,
                                          alignment: Alignment.center,
                                          decoration: BoxDecoration(
                                            color: Colors.grey.withOpacity(0.4),
                                            borderRadius:
                                                BorderRadius.circular(10),
                                          ),
                                          child: const Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Row(
                                                children: [
                                                  Icon(
                                                    Icons.history,
                                                    color: Colors.white,
                                                    size: 14,
                                                  ),
                                                  SizedBox(width: 5),
                                                  Text(
                                                    '浏览记录',
                                                    style: TextStyle(
                                                      color: Colors.white,
                                                      fontSize: 12,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              SizedBox(height: 5),
                                              Text(
                                                '看过的故事',
                                                style: TextStyle(
                                                  color: Colors.white70,
                                                  fontSize: 10,
                                                ),
                                              ),
                                            ],
                                          ).paddingOnly(
                                              top: 8,
                                              bottom: 8,
                                              left: 15,
                                              right: 15),
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ).marginOnly(left: 15, right: 15),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ];
            },
            pinnedHeaderSliverHeightBuilder: () {
              return MediaQuery.of(context).padding.top + 56;
            },
            onlyOneScrollInBody: true,
            body: SizedBox(
              width: double.infinity,
              height: double.infinity,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _tabBars(),
                  _tarBarView(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  _tabBars() {
    return Container(
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Color(0xffe5e5e5),
            width: 0.5,
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          TabBar(
            controller: _tabController,
            tabAlignment: TabAlignment.center,
            labelColor: Colors.black,
            unselectedLabelColor: CustomColor.unselectedColor,
            // 选择的样式
            labelStyle: const TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w500,
            ),
            unselectedLabelStyle: const TextStyle(
              fontSize: 16,
            ),
            isScrollable: true,
            indicatorSize: TabBarIndicatorSize.tab,
            indicator: const UnderlineTabIndicator(
              borderRadius: BorderRadius.all(Radius.circular(10)),
              borderSide: BorderSide(
                width: 3.0,
                color: CustomColor.primaryColor,
              ),
            ),
            indicatorPadding: const EdgeInsets.only(left: 10.0, right: 10.0),
            tabs: mineController.tabs.asMap().entries.map((entry) {
              int index = entry.key;
              String text = entry.value;
              return Tab(
                key: index == 0 ? _storyTabKey : null,
                text: text,
              );
            }).toList(),
            onTap: (index) {
              // 切换tab
              _tabController.animateTo(index);
            },
          ),
        ],
      ).paddingOnly(bottom: 5),
    );
  }

  //tab下的view
  _tarBarView() {
    return Expanded(
      child: TabBarView(
          controller: _tabController,
          // physics: const AlwaysScrollableScrollPhysics(),
          children: [
            Column(
              children: [
                Container(
                  decoration: const BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                        color: Color(0xffe5e5e5),
                        width: 0.5,
                      ),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      TextButton(
                        style: ButtonStyle(
                          overlayColor:
                              WidgetStateProperty.all(Colors.transparent),
                        ),
                        onPressed: () {
                          mineController.notesPublishType.value = 0;
                          mineController.onTap(TabsType.publish);
                        },
                        child: Text(
                          '公开${mineController.publishNotesNum.value == 0 ? '' : ' • ${mineController.publishNotesNum.value}'}',
                          style: TextStyle(
                            color: mineController.notesPublishType.value == 0
                                ? CustomColor.primaryColor
                                : CustomColor.unselectedColor,
                          ),
                        ),
                      ),
                      TextButton(
                        style: ButtonStyle(
                          overlayColor: WidgetStateProperty.all(
                              Colors.transparent), // 按下时的背景色
                        ),
                        onPressed: () {
                          mineController.notesPublishType.value = 1;
                          mineController.onTap(TabsType.private);
                        },
                        child: Text(
                          '私密${mineController.privateNotesNum.value == 0 ? '' : ' • ${mineController.privateNotesNum.value}'}',
                          style: TextStyle(
                            color: mineController.notesPublishType.value == 1
                                ? CustomColor.primaryColor
                                : CustomColor.unselectedColor,
                          ),
                        ),
                      ),
                      TextButton(
                        style: ButtonStyle(
                          overlayColor: WidgetStateProperty.all(
                              Colors.transparent), // 按下时的背景色
                        ),
                        onPressed: () {
                          mineController.notesPublishType.value = 2;
                          mineController.onTap(TabsType.draft);
                        },
                        child: Text(
                          '草稿${mineController.draftNotesNum.value == 0 ? '' : ' • ${mineController.draftNotesNum.value}'}',
                          style: TextStyle(
                            color: mineController.notesPublishType.value == 2
                                ? CustomColor.primaryColor
                                : CustomColor.unselectedColor,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Container(
                    decoration: const BoxDecoration(
                      color: Colors.transparent,
                    ),
                    child: Obx(
                      () => StaggeredGridView.countBuilder(
                        crossAxisCount: 2,
                        itemCount: mineController.myNotes.length,
                        mainAxisSpacing: 6,
                        crossAxisSpacing: 8,
                        padding: const EdgeInsets.all(10),
                        itemBuilder: (BuildContext context, int index) {
                          return mineController.notesPublishType.value == 2
                              ? GestureDetector(
                                  onTap: () {
                                    Get.log('点击了草稿');
                                    Map<String, dynamic> args = {
                                      'id': mineController.myNotes[index].id,
                                      'draft': true,
                                    };
                                    Get.toNamed('/publish/notes',
                                        arguments: args);
                                  },
                                  child: Container(
                                    width: double.infinity,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(10),
                                    ),
                                    child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: [
                                          ConstrainedBox(
                                            constraints: const BoxConstraints(
                                              minHeight: 0,
                                              maxHeight: 200,
                                              minWidth: double.infinity,
                                              maxWidth: double.infinity,
                                            ),
                                            child: Builder(builder: (context) {
                                              return Container(
                                                width: double.infinity,
                                                decoration: BoxDecoration(
                                                  borderRadius:
                                                      const BorderRadius.all(
                                                          Radius.circular(10)),
                                                  image: mineController
                                                              .myNotes[index]
                                                              .type ==
                                                          0
                                                      ? DecorationImage(
                                                          image: FileImage(
                                                            // 以逗号分割，取第一个
                                                            File(mineController
                                                                    .myNotes[
                                                                        index]
                                                                    .filesPath
                                                                    .contains(
                                                                        ',')
                                                                ? mineController
                                                                    .myNotes[
                                                                        index]
                                                                    .filesPath
                                                                    .split(
                                                                        ',')[0]
                                                                : mineController
                                                                    .myNotes[
                                                                        index]
                                                                    .filesPath),
                                                          ),
                                                          fit: BoxFit.cover,
                                                        )
                                                      : DecorationImage(
                                                          image: FileImage(
                                                            File(mineController
                                                                .myNotes[index]
                                                                .coverPath),
                                                          ),
                                                          fit: BoxFit.cover,
                                                        ),
                                                ),
                                              );
                                            }),
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text(
                                                DateShowUtil.showDateWithTime(
                                                    mineController
                                                        .myNotes[index]
                                                        .createTime),
                                                style: const TextStyle(
                                                  color: Color(0xffa3a3a2),
                                                  fontSize: 10,
                                                ),
                                              ),
                                              GestureDetector(
                                                onTap: () {
                                                  mineController.onDeleteDraft(
                                                      mineController
                                                          .myNotes[index].id);
                                                },
                                                child: const Icon(
                                                  Icons.delete_forever_rounded,
                                                  color: Color(0xffa3a3a2),
                                                  size: 16,
                                                ),
                                              ),
                                            ],
                                          ).paddingAll(5),
                                        ]),
                                  ),
                                )
                              : ItemView(
                                  id: mineController.myNotes[index]['id'],
                                  authorId: mineController.myNotes[index]
                                      ['belongUserId'],
                                  coverPicture: mineController.myNotes[index]
                                      ['coverPicture'],
                                  noteTitle: mineController.myNotes[index]
                                      ['title'],
                                  authorAvatar: mineController.myNotes[index]
                                      ['avatarUrl'],
                                  authorName: mineController.myNotes[index]
                                      ['nickname'],
                                  notesLikeNum: mineController.myNotes[index]
                                      ['notesLikeNum'],
                                  notesType: mineController.myNotes[index]
                                      ['notesType'],
                                  isLike: mineController.myNotes[index]
                                      ['isLike'],
                                );
                        },
                        staggeredTileBuilder: (int index) =>
                            const StaggeredTile.fit(1),
                      ),
                    ),
                  ),
                ),
              ],
            ),
            Container(
              decoration: const BoxDecoration(
                color: Colors.transparent,
              ),
              child: Obx(
                () => StaggeredGridView.countBuilder(
                  crossAxisCount: 2,
                  itemCount: mineController.myCollects.length,
                  mainAxisSpacing: 6,
                  crossAxisSpacing: 8,
                  padding: const EdgeInsets.all(10),
                  itemBuilder: (BuildContext context, int index) {
                    return ItemView(
                      id: mineController.myCollects[index]['id'],
                      authorId: mineController.myCollects[index]
                          ['belongUserId'],
                      coverPicture: mineController.myCollects[index]
                          ['coverPicture'],
                      noteTitle: mineController.myCollects[index]['title'],
                      authorAvatar: mineController.myCollects[index]
                          ['avatarUrl'],
                      authorName: mineController.myCollects[index]['nickname'],
                      notesLikeNum: mineController.myCollects[index]
                          ['notesLikeNum'],
                      notesType: mineController.myCollects[index]['notesType'],
                      isLike: mineController.myCollects[index]['isLike'],
                    );
                  },
                  staggeredTileBuilder: (int index) =>
                      const StaggeredTile.fit(1),
                ),
              ),
            ),
            Container(
              decoration: const BoxDecoration(
                color: Colors.transparent,
              ),
              child: Obx(
                () => StaggeredGridView.countBuilder(
                  crossAxisCount: 2,
                  itemCount: mineController.myLikes.length,
                  mainAxisSpacing: 6,
                  crossAxisSpacing: 8,
                  padding: const EdgeInsets.all(10),
                  itemBuilder: (BuildContext context, int index) {
                    return ItemView(
                      id: mineController.myLikes[index]['id'],
                      authorId: mineController.myLikes[index]['belongUserId'],
                      coverPicture: mineController.myLikes[index]
                          ['coverPicture'],
                      noteTitle: mineController.myLikes[index]['title'],
                      authorAvatar: mineController.myLikes[index]['avatarUrl'],
                      authorName: mineController.myLikes[index]['nickname'],
                      notesLikeNum: mineController.myLikes[index]
                          ['notesLikeNum'],
                      notesType: mineController.myLikes[index]['notesType'],
                      isLike: mineController.myLikes[index]['isLike'],
                    );
                  },
                  staggeredTileBuilder: (int index) =>
                      const StaggeredTile.fit(1),
                ),
              ),
            ),
            // 路线选项卡内容
            Container(
              decoration: const BoxDecoration(
                color: Colors.transparent,
              ),
              child: Obx(
                () => mineController.myRoutes.isEmpty
                    ? const Center(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(
                              Icons.route,
                              size: 64,
                              color: Colors.grey,
                            ),
                            SizedBox(height: 16),
                            Text(
                              '还没有创建过路线',
                              style: TextStyle(
                                color: Colors.grey,
                                fontSize: 16,
                              ),
                            ),
                          ],
                        ),
                      )
                    : ListView.builder(
                        padding: const EdgeInsets.all(16),
                        itemCount: mineController.myRoutes.length,
                        itemBuilder: (context, index) {
                          final route = mineController.myRoutes[index];
                          return GestureDetector(
                            onTap: () {
                              // 跳转到路线详情页面
                              Get.toNamed('/oneRouteMap', arguments: {
                                'routeId': route['id'],
                                'hideButtons': true, // 标记隐藏右边按钮
                              });
                            },
                            child: Container(
                              margin: const EdgeInsets.only(bottom: 12),
                              padding: const EdgeInsets.all(16),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(12),
                                boxShadow: [
                                  BoxShadow(
                                    color: Colors.black.withOpacity(0.1),
                                    blurRadius: 8,
                                    offset: const Offset(0, 2),
                                  ),
                                ],
                              ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      width: 4,
                                      height: 20,
                                      decoration: BoxDecoration(
                                        color: Color(int.parse(
                                          route['routeColor']?.toString().replaceFirst('#', '0xFF') ?? '0xFFFF5722',
                                        )),
                                        borderRadius: BorderRadius.circular(2),
                                      ),
                                    ),
                                    const SizedBox(width: 12),
                                    Expanded(
                                      child: Text(
                                        route['routeName']?.toString() ?? '未命名路线',
                                        style: const TextStyle(
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.black87,
                                        ),
                                      ),
                                    ),
                                    if (route['isPublic'] == 1)
                                      Container(
                                        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                                        decoration: BoxDecoration(
                                          color: Colors.green.withOpacity(0.1),
                                          borderRadius: BorderRadius.circular(4),
                                        ),
                                        child: const Text(
                                          '公开',
                                          style: TextStyle(
                                            fontSize: 12,
                                            color: Colors.green,
                                          ),
                                        ),
                                      ),
                                  ],
                                ),
                                const SizedBox(height: 8),
                                if (route['routeDescription']?.toString().isNotEmpty == true)
                                  Text(
                                    route['routeDescription'].toString(),
                                    style: const TextStyle(
                                      fontSize: 14,
                                      color: Colors.grey,
                                    ),
                                    maxLines: 2,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                const SizedBox(height: 12),
                                Row(
                                  children: [
                                    Icon(
                                      Icons.location_on,
                                      size: 16,
                                      color: Colors.grey[600],
                                    ),
                                    const SizedBox(width: 4),
                                    Text(
                                      '${route['notesCount'] ?? 0} 个地点',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Colors.grey[600],
                                      ),
                                    ),
                                    const SizedBox(width: 16),
                                    if (route['estimatedDuration'] != null && route['estimatedDuration'] > 0)
                                      Row(
                                        children: [
                                          Icon(
                                            Icons.access_time,
                                            size: 16,
                                            color: Colors.grey[600],
                                          ),
                                          const SizedBox(width: 4),
                                          Text(
                                            '${route['estimatedDuration']} 分钟',
                                            style: TextStyle(
                                              fontSize: 12,
                                              color: Colors.grey[600],
                                            ),
                                          ),
                                        ],
                                      ),
                                    const Spacer(),
                                    Text(
                                      route['createTime'] ?? "",
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Colors.grey[500],
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                            ),
                          );
                        },
                      ),
              ),
            ),
          ]),
    );
  }
}
